package com.sright.bcard.enterprise.web.controller;

import com.sright.bcard.common.service.share.dto.enterprise.EnterpriseDTO;
import com.sright.bcard.common.service.share.dto.page.PageDTO;
import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.common.WxappDTO;
import com.sright.bcard.common.share.service.WxappService;
import com.sright.bcard.content.share.service.TemplateService;
import com.sright.bcard.common.service.share.dto.enterprise.MaterialDTO;
import com.sright.bcard.common.service.share.enums.MaterialTypeEnum;
import com.sright.bcard.enterprise.share.response.MaterialResponse;
import com.sright.bcard.enterprise.share.service.EnterpriseService;
import com.sright.bcard.enterprise.share.service.MaterialService;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.user.share.request.ResetPasswordParam;
import com.sright.bcard.user.share.service.AdminService;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * setting controller
 *
 */
@RestController
@RequestMapping("/setting")
public class SettingController extends BaseController{

    @Autowired
    private AdminService adminService;

    @Autowired
    private MaterialService materialService;

    @Autowired
    private WxappService wxappService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 修改密码
     */
    @PutMapping("/resetPassword.json")
    @EnableLoginAuth
    public SrightResponse resetPassword(@Validated @RequestBody ResetPasswordParam body) {
        return adminService.updateAdminPassword(body.getOldPassword(), body.getNewPassword(), userContext.getUserId());
    }

    /**
     * 新增企业素材
     */
    @PostMapping("/material.json")
    @EnableLoginAuth
    public SrightResponse<Long> addMaterial(@Validated @RequestBody MaterialDTO materialDTO) {
        buildLoginUser(materialDTO);
        return materialService.addMaterial(materialDTO);
    }

    /**
     * 企业素材列表
     */
    @GetMapping("/material.json")
    @EnableLoginAuth
    public SrightResponse<PageDTO<MaterialResponse>> list(@RequestParam(required = false) Integer type) {
        MaterialDTO enterpriseMaterialDTO = new MaterialDTO();
        enterpriseMaterialDTO.setType(type);
        buildLoginUser(enterpriseMaterialDTO);
        return materialService.list(enterpriseMaterialDTO);
    }

    /**
     * 编辑企业素材
     */
    @PutMapping("/material.json")
    @EnableLoginAuth
    public SrightResponse<Integer> editMaterial(@RequestBody MaterialDTO materialDTO){
        buildLoginUser(materialDTO);
        return materialService.editMaterial(materialDTO);
    }

    /**
     * 删除企业素材
     */
    @DeleteMapping("/material.json")
    @EnableLoginAuth
    public SrightResponse<Integer> deleteMaterial(@RequestBody MaterialDTO materialDTO) {
        return SrightResponse.defaultSuccess(materialService.deleteMaterial(materialDTO.getId()));
    }

    /**
     * 获取企业背景图列表
     */
    @GetMapping("/videoCover.json")
    @EnableLoginAuth
    public SrightResponse<PageDTO<MaterialResponse>> videoCoverList() {
        MaterialDTO materialDTO = new MaterialDTO();
        materialDTO.setType(MaterialTypeEnum.PROPAGATE_VEDIO_BACKGROUND_PICATURE.getValue());
        buildLoginUser(materialDTO);
        return materialService.list(materialDTO);
    }

    /**
     * 新增企业背景图
     * @return
     */
    @PostMapping("/videoCover.json")
    @EnableLoginAuth
    public SrightResponse<Long> addVideoCover(@RequestBody MaterialDTO materialDTO) {
        materialDTO.setType(MaterialTypeEnum.PROPAGATE_VEDIO_BACKGROUND_PICATURE.getValue());
        buildLoginUser(materialDTO);
        return materialService.addMaterial(materialDTO);
    }

    /**
     * 删除企业背景图
     * @param materialDTO
     * @return
     */
    @DeleteMapping("/videoCover.json")
    @EnableLoginAuth
    public SrightResponse<Integer> deleteVideoCover(@RequestBody MaterialDTO materialDTO) {
        buildLoginUser(materialDTO);
        return SrightResponse.defaultSuccess(materialService.deleteMaterial(materialDTO.getId()));
    }

    /**
     * 修改企业背景图
     * @return
     */
    @PutMapping("/videoCover.json")
    @EnableLoginAuth
    public SrightResponse<Integer> updateVideoCover(@RequestBody MaterialDTO materialDTO) {
        materialDTO.setType(MaterialTypeEnum.PROPAGATE_VEDIO_BACKGROUND_PICATURE.getValue());
        buildLoginUser(materialDTO);
        return materialService.editMaterial(materialDTO);
    }

    /**
     * 获取企业宣传视频列表
     * @return
     */
    @GetMapping("/corporateVideo.json")
    @EnableLoginAuth
    public SrightResponse<PageDTO<MaterialResponse>> corporateVideoList() {
        MaterialDTO materialDTO = new MaterialDTO();
        materialDTO.setType(MaterialTypeEnum.ENTERPRISE_PROPAGATE_VEDIO.getValue());
        buildLoginUser(materialDTO);
        return materialService.list(materialDTO);
    }

    /**
     * 新增企业宣传视频
     * @return
     */
    @PostMapping("/corporateVideo.json")
    @EnableLoginAuth
    public SrightResponse<Long> addCorporateVideo(@RequestBody MaterialDTO materialDTO) {
        materialDTO.setType(MaterialTypeEnum.ENTERPRISE_PROPAGATE_VEDIO.getValue());
        buildLoginUser(materialDTO);
        return materialService.addMaterial(materialDTO);
    }

    /**
     * 删除企业宣传视频
     * @return
     */
    @DeleteMapping("/corporateVideo.json")
    @EnableLoginAuth
    public SrightResponse<Integer> deleteCorporateVideo(@RequestBody MaterialDTO materialDTO) {
        return SrightResponse.defaultSuccess(materialService.deleteMaterial(materialDTO.getId()));
    }

    /**
     * 修改企业宣传视频
     * @return
     */
    @PutMapping("/corporateVideo.json")
    @EnableLoginAuth
    public SrightResponse<Integer> updateCorporateVideo(@RequestBody MaterialDTO materialDTO) {
        materialDTO.setType(MaterialTypeEnum.ENTERPRISE_PROPAGATE_VEDIO.getValue());
        buildLoginUser(materialDTO);
        return materialService.editMaterial(materialDTO);
    }

    /**
     * 获取绑定微信小程序列表
     * @param pageParamRequest
     * @return
     */
    @GetMapping("/wxapp.json")
    @EnableLoginAuth
    public SrightResponse<PageDTO<WxappDTO>> wxappList(PageParamRequest pageParamRequest) {
        pageParamRequest.setEnterpriseId(userContext.getEnterpriseId());
        pageParamRequest.setUserId(userContext.getUserId());
        pageParamRequest.setToken(userContext.getToken());
        return wxappService.wxappList(pageParamRequest);
    }

    /**
     * 修改绑定微信小程序状态
     * @param wxappDTO
     * @return
     */
    @PutMapping("/status.json")
    @EnableLoginAuth
    public SrightResponse<Integer> status(@RequestBody WxappDTO wxappDTO) {
        wxappDTO.setEnterpriseId(userContext.getEnterpriseId());
        wxappDTO.setUserId(userContext.getUserId());
        wxappDTO.setToken(userContext.getToken());
        return wxappService.status(wxappDTO);
    }

    /**
     * 删除微信小程序绑定
     * @param wxappDTO
     * @return
     */
    @DeleteMapping("/status.json")
    @EnableLoginAuth
    public SrightResponse<Integer> deleteStatus(@RequestBody WxappDTO wxappDTO) {
        return wxappService.deleteStatus(wxappDTO.getId());
    }

    /**
     * 全量查询template列表
     * @return
     */
    @GetMapping("/template.json")
    @EnableLoginAuth
    public SrightResponse templateList() {
        return templateService.allTemplateList();
    }

    @PutMapping("/onStatus.json")
    @EnableLoginAuth
    public SrightResponse onStatus(@RequestBody EnterpriseDTO enterpriseDTO) {
        enterpriseDTO.setEnterpriseId(userContext.getEnterpriseId());
        return enterpriseService.onStatus(enterpriseDTO);
    }

    private void buildLoginUser(MaterialDTO enterpriseMaterialDTO) {
        enterpriseMaterialDTO.setUserId(userContext.getUserId());
        enterpriseMaterialDTO.setToken(userContext.getToken());
        enterpriseMaterialDTO.setEnterpriseId(userContext.getEnterpriseId());
    }
}
